package com.lxc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lxc.entity.Result;
import com.lxc.entity.Works;
import com.lxc.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/work")
@CrossOrigin
public class WorkController {

    @Autowired
    private WorkService workService;
//    接收所有work
 /*   @GetMapping()
    public List<Works> getAllWork(){
        return workService.list();
    }*/

    //根据id查询详细信息
    @GetMapping("/{workid}")
    public Result<Works> getWorkById(@PathVariable Integer workid){
        return Result.success(workService.getWorkById(workid));
    }


    @GetMapping
    /* 分页查询，种类*/
    public Result<IPage<Works>> getWorks(@RequestParam(required = false) String name, @RequestParam(required = false) String category,
                                               @RequestParam(defaultValue = "1") Integer currentPage,  @RequestParam(defaultValue = "5") Integer pageSize) {
        return Result.success(workService.getWorks(name, category,currentPage, pageSize));
    }


    @PostMapping(path = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<Works> addWork(@RequestPart("works") Works works,
                                 @RequestParam("file") MultipartFile file) throws IOException {
        Works work=workService.addwork(works,file);
        return Result.success(work);
    }

    @DeleteMapping("/{id}")
    public Result deleteWork(@PathVariable Integer id){
        boolean flag =workService.deleteById(id);
        return flag?Result.success("删除成功"):Result.error( 500,"删除失败");
    }


}